<?php
// 本类由系统自动生成，仅供测试用途
class ArticleSortAction extends BaseAction {
    public function __construct() {
        parent::__construct();
        //权限判定
        
        //语言包设置
        $language['ch']=array(
            //头部
            "titlebt"=>'栏目管理',
            "now_path"=>'当前位置',
            "header_admin"=>"栏目管理",
            "header_add"=>"添加栏目",
            "header_value1"=>"在这里，你可以设置",
            "header_value2"=>"栏目",
            "header_value3"=>"其他3",
            "header_value4"=>"其他4",
            "header_value5"=>"其他5",
            "header_value6"=>"其他6",
            "titlebt2"=>"栏目",
        );
        
        //解析语言包
        $this->assign("language",$language['ch']);
        
        
    }
    public function index(){
        $sort=D("ArticleSort");
        $articlesort=$sort
                ->order("article_sort_path")
                ->select();
        $maxlevel=1;
        foreach($articlesort as $key=>$vo){
            $num=substr_count($vo['article_sort_path'],',');
            $articlesort[$key]['level']=$num-1;
            if($maxlevel<$num-1){
                $maxlevel=$num-1;
            }
        }
        $articlesort[0]['maxlevel']=$maxlevel;
        //print_r($articlesort);
        $this->assign("articlesort",$articlesort);
        $this->display();
    }
    
    public function insert(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $this->assign("sid",  intval($_GET['id']));
        }
        $sort=D("ArticleSort");
       $articlesort=$sort
                ->order("article_sort_path")
                ->select();
        $maxlevel=1;
        foreach($articlesort as $key=>$vo){
            $num=substr_count($vo['article_sort_path'],',');
            $articlesort[$key]['level']=$num-1;
            if($maxlevel<$num-1){
                $maxlevel=$num-1;
            }
        }
        $articlesort[0]['maxlevel']=$maxlevel;
       
        $this->assign("articlesort",$articlesort);  
        $this->display();
    }
    
    public function doinsert(){
        $sort=D("ArticleSort");
        if(isset($_POST['article_sort_name'])&&$_POST['article_sort_name']!=''){
            $insert_arr=Array(
                "article_sort_name"=>$_POST['article_sort_name'],
                "article_sort_parent_id"=>$_POST['article_sort_parent_id'],
                "place"=>$_POST['place'],
                "article_sort_status"=>$_POST['article_sort_status'],
                "article_sort_ordering"=>$_POST['article_sort_ordering'],
                "article_sort_url"=>$_POST['article_sort_url'],
                "article_sort_theme"=>$_POST['article_sort_theme'],
                "article_sort_content"=>$_POST['article_sort_content'],
                "article_sort_img"=>$_POST['article_sort_img'],
                
            );
            //echo $_POST['article_sort_parent_id'];
            $parent=$sort->where(array('article_sort_id'=>$_POST['article_sort_parent_id']))->find();
            //print_r($parent);
            if($parent){
                $path=$parent['article_sort_path'];
                //echo $path;
            }else{
                $path=",0";
            }
            $sort->startTrans();
            $insert=$sort->data($insert_arr)->add();
            if($insert){
                $path=$path.",".$insert;
               // echo $path;
                $update_arr=Array('article_sort_path'=>$path);
                $update=  $sort->where(array('article_sort_id'=>$insert))->data($update_arr)->save();
                if($update){
                    $sort->commit();
                    $this->assign("jumpUrl",U('index'));
                    $this->success("添加成功");
                }else{
                    $sort->rollback();
                    $this->error("添加失败");
                }
            }else{
                $sort->rollback();
                $this->error("添加失败");
            }
        }
        
    }
    public function update(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=intval($_GET['id']);
            $this->assign("id",  $id);
            $sid=intval($_GET['id']);
            $sort=D("ArticleSort");
            
            $articlesort=$sort
                ->order("article_sort_path")
                ->select();
            $maxlevel=1;
            foreach($articlesort as $key=>$vo){
                $num=substr_count($vo['article_sort_path'],',');
                $articlesort[$key]['level']=$num-1;
                if($maxlevel<$num-1){
                    $maxlevel=$num-1;
                }
                if($vo['article_sort_id']==$id){
                    $this->assign("sort_vo",$vo);
                }
            }
            $articlesort[0]['maxlevel']=$maxlevel;
            
            $this->assign("articlesort",$articlesort);  
        
            $this->display();
        }else{
            $this->error("数据错误");
        }
        
    }
    public function doupdate(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=  intval($_GET['id']);
        }else{
            $this->error("数据错误");
        }
        if(isset($_POST['article_sort_name'])&&$_POST['article_sort_name']!=''){
            $sort=D("ArticleSort");
            $parent=$sort->where(array('article_sort_id'=>$_POST['article_sort_parent_id']))->find();
            if($parent){
                $path=$parent['article_sort_path'];
            }else{
                $path=",0";
            }
            $path.=",".$id;
            $update_arr=Array(
                "article_sort_name"=>$_POST['article_sort_name'],
                "place"=>$_POST['place'],
                "article_sort_status"=>$_POST['article_sort_status'],
                "article_sort_ordering"=>$_POST['article_sort_ordering'],
                "article_sort_parent_id"=>$_POST['article_sort_parent_id'],
                "article_sort_url"=>$_POST['article_sort_url'],
                "article_sort_theme"=>$_POST['article_sort_theme'],
                "article_sort_content"=>$_POST['article_sort_content'],
                "page_num"=>$_POST['page_num'],
                "article_theme"=>$_POST['article_theme'],
                "article_sort_short_content"=>$_POST['article_sort_short_content'],
                "article_sort_img"=>$_POST['article_sort_img'],
                "article_sort_path"=>$path,
            );
            $update=$sort->where(array('article_sort_id'=>$id))->data($update_arr)->save();
            if($update){
                $this->assign("jumpUrl",U('index'));
                $this->success("文章栏目编辑成功");
            }else{
                $this->error("文章栏目编辑失败");
            }
        }else{
            $this->error("数据错误");
        }
    }
    public function del(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=intval($_GET['id']);
            $sort=D("ArticleSort");
            $del=$sort
                ->where(array("article_sort_id"=>$id))
                ->delete();
            if($del){
                $this->success("删除成功");
                
            }else{
                $this->error("删除失败");
            }
        }else{
            $this->error("数据错误");
        }
    }
    
}